package com.example.roundprogressbar.slice;

import com.example.roundprogressbar.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.RoundProgressBar;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

import java.util.Timer;
import java.util.TimerTask;

public class MainAbilitySlice extends AbilitySlice {
    private TimerTask timerTask;
    private Timer timer;
    private static final int DELAY_TIME = 1000;
    private static final int PERIOD = 1000;
    private static final int EVENT_ID = 0x12;
    private int NUMBER = 5;
    private RoundProgressBar round_progress_bar;
    private RoundProgressBar round_progress_bar2;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        round_progress_bar = (RoundProgressBar)findComponentById(ResourceTable.Id_round_progress_bar);
        round_progress_bar2 = (RoundProgressBar)findComponentById(ResourceTable.Id_round_progress_bar2);
        startTask();
    }

    private void startTask() {
        timerTask = new TimerTask() {
            @Override
            public void run() {
                handler.sendEvent(EVENT_ID);
            }
        };
        timer = new Timer();
        timer.schedule(timerTask, DELAY_TIME, PERIOD);
    }

    private EventHandler handler = new EventHandler(EventRunner.current()) {
        @Override
        protected void processEvent(InnerEvent event) {
            if (event.eventId == EVENT_ID) {
                NUMBER = NUMBER + 5;
                if(NUMBER == 100){
                    NUMBER = 5;
                }
                round_progress_bar.setProgressValue(NUMBER);
                round_progress_bar2.setStartAngle(NUMBER);
            }
        }
    };

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
